package com.itfreer.data.organize;

import android.util.Log;

import com.itfreer.data.SysDatabaseHelper;
import com.itfreer.data.organize.entity.AdministrativeEntity;
import com.itfreer.data.organize.entity.OrganizationEntity;
import com.itfreer.data.remote.DataService;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.misc.TransactionManager;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 组织机构同步服务类
 */
public class OrganizeSynService {
    private static final String administrativeSN = "administrative";
    private static final String organizationSN = "organization";
    private static final Integer pageSize = 100;

    public static final OrganizeSynService instance = new OrganizeSynService();
    private OrganizeSynService() {

    }

    /**
     * 同步政区
     * @return
     */
    public Boolean synAdministrative() {
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(SysDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<AdministrativeEntity, String> dao = SysDatabaseHelper.getInstance().getDao(AdministrativeEntity.class);
                // 清空表
                dao.queryRaw("delete from administrative");

                // 从远程获取表总数
                Integer rowCount = DataService.instance.getRowCounts(administrativeSN, null);
                Integer pageCount = (rowCount / pageSize) + (rowCount % pageSize > 0 ? 1 : 0);

                // 从远程获取表记录
                for(Integer pageIndex = 1;pageIndex<=pageCount;pageIndex++){
                    List<AdministrativeEntity> datas = DataService.instance.getEntitys(administrativeSN,null,null,null, pageSize, pageIndex, AdministrativeEntity.class);
                    if(datas!=null && datas.size()>0){
                        for(AdministrativeEntity item : datas){
                            dao.create(item);
                        }
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 同步机构
     * @return
     */
    public Boolean synOrganization() {
        //创建事务管理器
        TransactionManager transactionManager = new TransactionManager(SysDatabaseHelper.getInstance().getConnectionSource());
        //一个调用的事件
        Callable<Boolean> callable  = new Callable<Boolean>() {
            //如果异常被抛出 事件管理 就知道保存数据失败要回滚
            @Override
            public Boolean call() throws Exception {
                Dao<OrganizationEntity, String> dao = SysDatabaseHelper.getInstance().getDao(OrganizationEntity.class);
                // 清空表
                dao.queryRaw("delete from organization");

                // 从远程获取表总数
                Integer rowCount = DataService.instance.getRowCounts(organizationSN, null);
                Integer pageCount = (rowCount / pageSize) + (rowCount % pageSize > 0 ? 1 : 0);

                // 从远程获取表记录
                for(Integer pageIndex = 1;pageIndex<=pageCount;pageIndex++){
                    List<OrganizationEntity> datas = DataService.instance.getEntitys(organizationSN,null,null,null, pageSize, pageIndex, OrganizationEntity.class);
                    if(datas!=null && datas.size()>0){
                        for(OrganizationEntity item : datas){
                            dao.create(item);
                        }
                    }
                }
                return true;
            }
        };

        Boolean result;
        try {
            //执行事件
            result =  transactionManager.callInTransaction(callable);
        } catch (SQLException e) {
            result = false;
            Log.w("YKF","事务保存异常");
            e.printStackTrace();
        }
        return result;
    }
}
